#coding=utf-8

require "aio/core"

class Aio::Module::Cmd::Maipu::ShowIpRoute < Aio::Module::Cmd::Maipu
	include Aio::Module

	def initialize
		super({
			:cmd_full			=> "show ip route",
			:cmd_short		=> "sh ip rou",
			:author				=> "Elin",
			:ranking			=> Ranking_2,
			:description	=> "This is Maipu Command# show ip route",
			:platform			=> "all",
			:benchmark		=> {}
		})
	end

	def parse
		cont = self.context.dup
		routing = {}
		useful[:routing] = routing

		cont.readline_match_block(reg_blank) {|b|b}
		cont.readline_match_block(reg_blank) {|b|b}

		cont.readline_range_if_loop(/^\w/, /^\s+/) do |cont_layer|
			parse_routing(cont_layer)
		end
	end

	def parse_routing(context)
		
		router = nil

		if context[0] =~ /^C/
			context.readline_match_block(/C (?<dest>.*) is directly connected, .*, (?<iface>.*)/) do |block|
				route = {}
				router = block[:dest]
				useful[:routing][router] = {}
				useful[:routing][router][:first] = route
				block.update(useful[:routing][router], :dest)
				block.update(route, :proto, "C")
				block.update(route, :iface)
			end
			return
		end

		context.readline_match_block(/(?<proto>.*) (?<dest>.*) \[(?<metric>\d+)\/(?<ad>\d+)\] via (?<next_hop>.*), (?<time>.*), (?<iface>.*)/) do |block|
			route = {}
			router = block[:dest]
			useful[:routing][router] = {}
			useful[:routing][router][:first] = route
			block.update(useful[:routing][router], :dest)
			block.update(route, :proto)
			block.update(route, :metric)
			block.update(route, :ad)
			block.update(route, :next_hop)
			block.update(route, :iface)
		end

		if context.size == 2
			context.readline_match_block(/\[(?<metric>\d+)\/(?<ad>\d+)\] via (?<next_hop>.*), (?<time>.*), (?<iface>.*)/) do |block|
				route = {}
				useful[:routing][router][:secend] = route
				block.update(route, :metric)
				block.update(route, :ad)
				block.update(route, :next_hop)
				block.update(route, :iface)
			end
		end
	end
end
